package cn.niit.smbms.service.impl;

import cn.niit.smbms.dao.UserMapper;
import cn.niit.smbms.entity.User;
import cn.niit.smbms.service.UserService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名和密码查询数据库中是否有符合条件的记录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User selectByUserNamePassword(String username, String password) {
        User user = userMapper.selectByUserCodePassword(username, password);
        return user;
    }

    /**
     * 根据查询条件（已封装为user对象），查询符合所有条件的用户
     * @param user
     * @return
     */
    @Override
    public List<User> selectListByUser(User user) {
        /** 开启分页查询，page = 当前第几页， rows = 每页记录数 */
        PageHelper.startPage(user.getPage(), user.getRows());
        return userMapper.selectListByUser(user);
    }

    /**
     * 根据用户Id查询用户信息
     * @param userId
     * @return
     */
    @Override
    public User selectById(Long userId) {
        return userMapper.selectById(userId);
    }


    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public int add(User user) {
        user.setGmtCreate(new Date());
        log.info("获取到的userMapper:",userMapper);
        int rows = userMapper.insertUser(user);
        return rows;
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }


    /**
     * 删除用户
     * @param userId
     * @return
     */
    @Override
    public int deleteById(Long userId) {
        return userMapper.deleteById(userId);
    }


}
